package com.hddf.project.auth.service;

import org.kommii.encryption.MD5Handler;

import com.hddf.project.auth.dao.AuthorityDao_MongoDB;
import com.hddf.project.auth.dao.UserDao_MongoDB;
import com.hddf.project.auth.dao_impl.AuthorityDaoImpl_MongoDB;
import com.hddf.project.auth.dao_impl.UserDaoImpl_MongoDB;
import com.hddf.project.auth.pojos.Authority;
import com.hddf.project.auth.pojos.User;
import com.kommii.mongodb_utils.Mongodb_ConnPools;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;



public class MongoDB_Sync {
	
	private static MongoDB_Sync mongo_sync = null;
	private static UserDao_MongoDB user_dao_mongodb = null;
	private static AuthorityDao_MongoDB auth_dao_mongodb = null;
	
	static {
		if(mongo_sync==null){
			mongo_sync = new MongoDB_Sync();
		}
		if(user_dao_mongodb==null){
			user_dao_mongodb = new UserDaoImpl_MongoDB();
		}
		if(auth_dao_mongodb==null){
			auth_dao_mongodb = new AuthorityDaoImpl_MongoDB();
		}
	}
	
	public static MongoDB_Sync getInstance(){
		return mongo_sync;
	}
	
	public synchronized boolean  addUser(User u){
		DBCollection coll  = null;
		try{
			//String password = u.getPassword();
			//String passwd = MD5Handler.encrypt(password);
			//u.setPassword(passwd);
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			boolean rs = user_dao_mongodb.save(u, coll);
			return rs; 
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public synchronized boolean updateUser(User u){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			boolean rs = user_dao_mongodb.update(u, coll);
			return rs;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public synchronized boolean updateUser(DBObject obj){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			boolean rs = user_dao_mongodb.update(obj, coll);
			return rs;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public synchronized String updateUserPassword(String username ,String new_pw,String old_pw){
		DBCollection coll  = null;
		String password = "";
		String msg = "0";
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			User u = user_dao_mongodb.getUserByName(username, coll);
			if(null!=u){
				password = u.getPassword();
			}else{
				msg = username+ " 用户不存在, 请核对用户名";
				return msg;
			}
			
			String temp = MD5Handler.encrypt(old_pw); 
			if(password.equals(temp)){
				String new_password = MD5Handler.encrypt(new_pw);
				u.setPassword(new_password);
				boolean rs = user_dao_mongodb.updatePassword(u,coll);
				if(rs){
					msg = "1";
				}else{
					msg = "0";
				}
			}else{
				msg = "原密码不正确";
			}
			return msg;
		}catch(Exception e){
			e.printStackTrace();
			return "0";
		}
	}
	
	public synchronized boolean deleteUser(User u){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			int user_id = u.getId();
			boolean rs = user_dao_mongodb.delete(user_id, coll);
			return rs;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public synchronized boolean setUserOff(int id){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			boolean rs = user_dao_mongodb.setUserOff(id, coll);
			return rs;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public synchronized boolean setUserOn(int id){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(User.mongodb_collection_name);
			boolean rs = user_dao_mongodb.setUserOn(id, coll);
			return rs;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
/*************权限部分****************/
	
	public synchronized boolean  addAuth(DBObject auth){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(Authority.mongodb_collection_name);
			boolean rs = auth_dao_mongodb.save(auth, coll);
			return rs; 
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public synchronized boolean  updateAuth(DBObject auth){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(Authority.mongodb_collection_name);
			boolean rs = auth_dao_mongodb.update(auth, coll);
			return rs; 
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	public synchronized boolean  deleteAuth(int id){
		DBCollection coll  = null;
		try{
			coll = Mongodb_ConnPools.getMongoDB_Coll(Authority.mongodb_collection_name);
			boolean rs = auth_dao_mongodb.delete(id,coll);
			return rs; 
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	
	


}
